﻿/* 
    SignalR script to update the chat page and send messages.
*/

// Variable group is used to only send messages to those in your game room.
var group = null;
var masterHub = null;
var gameRoomModel = null;
var currentUserID = null;

// Initalizing function that is called with id of gameroom as parameter.
function initializeGameRoom(id, hub, model, player) {
    group = id;
    masterHub = hub;
    gameRoomModel = model;
    currentUserID = player;
    
    window.onbeforeunload = function () {
        gameRoomModel.slots()[gameRoomModel.slotNumber].hubConnectionID = null;
        gameRoomModel.slots()[gameRoomModel.slotNumber].isCPU = false;
        gameRoomModel.slots()[gameRoomModel.slotNumber].occupied = false;
        gameRoomModel.slots()[gameRoomModel.slotNumber].ready = false;
        gameRoomModel.slots()[gameRoomModel.slotNumber].userName = "";
        gameRoomModel.slots()[gameRoomModel.slotNumber].userProfileID = 0;
        masterHub.server.updateSlot(group, JSON.stringify(gameRoomModel.slots()[gameRoomModel.slotNumber]), self.gameRoomID).done(function () {
            return true;
        });
        return "You left the game! Quitter!";
    }

    // Create a function that the hub can call back to display messages.
    masterHub.client.addMessage = function (name, message) {
        // Add the message to the page. 
        $('#discussion').prepend('<li><strong>' + htmlEncode(name)
            + '</strong>: ' + htmlEncode(message) + '</li>');
    };

    // Function for updating gameroom UI.
    masterHub.client.updateName = function (name) {
        // Update model to new name. 
        gameRoomModel.gameRoomName(name);
    };

    masterHub.client.updateSlots = function (s) {
        // Update model slots. 
        gameRoomModel.slots(JSON.parse(s));
    };

    masterHub.client.updateState = function (s) {
        // Update model to state. 
        gameRoomModel.gameRoomState(s);
    };

    masterHub.client.error = function (s) {
        // Plain old alert for errors. 
        alert(s);
    };

    // Set initial focus to message input box.  
    $('#message').focus();

    // Start the connection.
    $.connection.hub.start().done(function () {
        masterHub.server.join(group);

        // Update player slots.
        masterHub.server.updateSlots(group, JSON.stringify(gameRoomModel.slots()), self.gameRoomID);

        $('#sendmessage').click(function () {
            if ($('#message').val() != '') {
                // Call the Send method on the hub. 
                masterHub.server.sendMessage(group, $('#message').val());
                // Clear text box and reset focus for next comment. 
                $('#message').val('').focus();
            }
        });
    });
}

// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
    if (value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;
    }
    else {
        return '';
    }
}